package com.swf.common.jar;

import java.util.*;
import java.util.Map.Entry;

import com.swf.common.base.*;
import com.swf.common.utils.*;

public class ClassInfo {
	private String id;
	private String from;
	private String to;
	private Map<String,Jar>  jarMap = new HashMap<String,Jar>();
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	
	public void setJarMap(Map<String, Jar> jarMap) {
		this.jarMap = jarMap;
	}
	public Map<String, Jar> getJarMap() {
		return jarMap;
	}
	/**
	 * 获取指定名称的jar
	 * @param name
	 * @return
	 */
	public Jar getJar(String name){
		return jarMap.get(name);
	}
	
	/**
	 * 添加一个jar
	 * @param jar
	 * @return
	 */
	public Jar addJar(Jar jar){
		return jarMap.put(jar.getName(),jar);
	}
	
	/**
	 * 删除一个jar
	 * @param jar
	 * @return
	 */
	public Jar removejar(Jar jar){
		return jarMap.remove(jar.getName());
	}
	/**
	 * 删除一个指定名称的jar
	 * @param name
	 * @return
	 */
	public Jar removejar(String	name){
		return jarMap.remove(name);
	}
	/**
	 *  返回ClassInfo的XML格式,相关字段不进行XML转义
	 */
	@Override
	public String toString() {
		return toString(0);
	}
	
	/**
	 * 返回ClassInfo的XML格式,相关字段from,to进行XML转义
	 * @return
	 */
	public String toEscapeString() {
		return toEscapeString(0);
	}
	/**
	 * 返回ClassInfo的XML格式,相关字段from,to进行XML转义
	 * @param tabCount
	 * @return
	 */
	public String toEscapeString(int tabCount) {
		String tab = FwsStringUtils.repeat(" ", tabCount);
		StringBuffer sb = new StringBuffer();
		sb.append(FwsStringUtils.addBefore("<classInfo id=\""+this.id+"\"",tab));
		sb.append(" from=\"" + FwsStringEscapeUtils.escapeXml(this.from)+"\"");
		sb.append(" to=\""+FwsStringEscapeUtils.escapeXml(this.to)+"\">");
		sb.append(FwsConstants.line_separator);
		Iterator<Entry<String, Jar>> it = jarMap.entrySet().iterator();
		while (it.hasNext()){
			sb.append(FwsStringUtils.addBefore(it.next().getValue().toEscapeString(4),tab));
			sb.append(FwsConstants.line_separator);
		}
		sb.append(FwsStringUtils.addBefore("</classInfo>",tab));
		return sb.toString();
	}
	/**
	 * 返回ClassInfo的XML格式,相关字段不进行XML转义
	 * @param tabCount
	 * @return
	 */
	public String toString(int tabCount){
		return FwsStringEscapeUtils.unescapeXml(toEscapeString(tabCount));
	}
}
